<template>
  <!-- 新六项措施存在的问题 -->
  <a-spin :spinning="loading">
    <title-name :title="`${$route.query.month}月新六项措施存在的问题`"></title-name>
    <!-- 列表 -->
    <Empty v-if="!Object.keys(list) || !Object.keys(list).length" description="暂无数据" />
    <div style="padding: 20px">
      <div class="mb20" v-for="(item, index) in list" :key="index">
        <title-name :title="index"></title-name>
        <div v-for="(i, idx) in item" :key="i.id">
          <div class="branchName">{{ idx }}</div>
          <a-table rowKey="id" bordered :pagination="false" :columns="columns" :dataSource="i"> </a-table>
        </div>
      </div>
    </div>
    <!-- 编辑审批及费用认定 -->
    <DialogForm ref="dialogForm" :visible.sync="visible" edit-model="E" headerTitle="编辑新六项措施存在的问题" :width="1000" :form="form" :formList="formList" :rules="rules" @confirm="submitForm" />
  </a-spin>
</template>

<script>
import { getSixProblem, updateSixProblem } from '@/api/quality/qualityMonth' // api
import { Empty } from 'ant-design-vue' // 无数据提示
import DialogForm from '@/components/DialogForm' // 表单弹窗

export default {
  name: 'SixMeasuresProblem',
  components: {
    Empty,
    DialogForm
  },
  props: {
    // 请求参数
    queryParam: {
      type: Object,
      default: () => null
    }
  },
  data() {
    return {
      loading: false, // 加载控件
      form: {}, // 编辑表单
      visible: false, // 弹窗显隐控件
      rules: {
        way: [{ required: true, whitespace: true, message: '请输入采取的对策', trigger: 'change' }]
      }, // 校验规则
      formList: [{ type: 'BaseTextarea', keyName: 'way', label: '采取的对策', span: 24 }], // 弹窗表单配置项
      list: {} // 列表
    }
  },
  computed: {
    // 表头
    columns() {
      let columns = [
        {
          title: '序号',
          width: 80,
          dataIndex: 'index',
          align: 'center',
          customRender() {
            return arguments[2] + 1
          }
        },
        {
          title: '项目名称',
          width: 200,
          dataIndex: 'projectName'
        },
        {
          title: '主要存在问题',
          width: 200,
          dataIndex: 'problemDesc'
        },
        {
          title: '现场照片',
          dataIndex: 'fileVOS',
          width: 150,
          customRender: (text) => {
            return text?.[0] ? <l-table disabled={true} uploadType="image" uploadArrys={text} /> : ''
          }
        },
        {
          title: '采取对策',
          dataIndex: 'way'
        }
      ]
      // 节点若为分公司则添加操作按钮
      this.queryParam.branchComId &&
        (this.$route.query.editModel == 2 || this.$route.query.editModel == 4) &&
        columns.push({
          title: '操作',
          dataIndex: 'actions',
          width: '120px',
          align: 'center',
          customRender: (text, record) => {
            return (
              <a
                onClick={() => {
                  this.form.way = record.way
                  this.form.id = record.id
                  this.visible = true
                }}
              >
                编辑
              </a>
            )
          }
        })
      return columns
    }
  },
  mounted() {
    // 初始化
    this.init()
  },
  methods: {
    /**
     * 初始化
     */
    init() {
      this.loading = true
      // 请求检查情况
      getSixProblem(this.queryParam)
        .then((res) => {
          if (res?.code === '0') {
            this.list = res.data || {}
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    /**
     * 提交表单
     * form：表单
     */
    submitForm(form) {
      this.$refs.dialogForm.loading = true
      form.qualityMonthId = this.$route.query.id
      form.isAdjust = this.$route.query.isAdjust
      updateSixProblem(form)
        .then((res) => {
          if (res.code === '0') {
            this.init()
            this.$message.success('提交成功！')
            this.visible = false
          }
        })
        .finally(() => {
          this.$refs.dialogForm.loading = false
        })
    }
  }
}
</script>
